package com.qianchang.microfinance.view.dialog.alert;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.view.dialog.DimengBaseDialog;
import com.qianchang.microfinance.utils.DMConstant;
import com.dm.library.utils.DensityUtil;
import com.dm.library.utils.StringUtils;


/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: <两个按钮的确认弹框>
 * 修 改 人: sushuo
 * 修改时间: 2017-3-8
 */

public class DimengConfirmAlertDialog extends DimengBaseDialog implements View.OnClickListener {

    private OnDialogClickListener onDialogClickListener;
    private Dialog mDialog;


    public static DimengConfirmAlertDialog newInstance(String title, String msg) {
        Bundle bundle = new Bundle();
        bundle.putString(DMConstant.StoreConstant.Title, title);
        bundle.putString(DMConstant.StoreConstant.Message, msg);
        DimengConfirmAlertDialog dimengAlertDialog = new DimengConfirmAlertDialog();
        dimengAlertDialog.setArguments(bundle);
        return dimengAlertDialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mDialog = getDialog();
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setCanceledOnTouchOutside(false);
        return inflater.inflate(R.layout.info_alert_dialog_two, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View view = getView();
        int width = DensityUtil.getScreenWidth(getActivity());
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = (int) (width * 0.85);
        view.setLayoutParams(params);

        TextView tv_title = (TextView) view.findViewById(R.id.dialog_title);
        TextView tv_message = (TextView) view.findViewById(R.id.dialog_message);
        TextView btn_agree = (TextView) view.findViewById(R.id.btn_dialog_agree);

        if (!TextUtils.isEmpty(agreeText)) {
            btn_agree.setText(agreeText);
        }
        TextView btn_dialog_cancel = (TextView) view.findViewById(R.id.btn_dialog_cancel);
        if (!TextUtils.isEmpty(notAgreeText)) {
            btn_dialog_cancel.setText(notAgreeText);
        }
        Bundle arguments = getArguments();
        if (arguments != null) {
            String s_title = arguments.getString(DMConstant.StoreConstant.Title, DMConstant.StoreConstant.NULL);
            String s_message = arguments.getString(DMConstant.StoreConstant.Message, DMConstant.StoreConstant.NULL);
            if (!StringUtils.isEmptyOrNull(s_title)) {
                tv_title.setText(s_title);
            } else {
                tv_title.setVisibility(View.GONE);
            }
            if (!StringUtils.isEmptyOrNull(s_message)) {
                tv_message.setText(s_message);
            }
        }
        btn_agree.setOnClickListener(this);
        btn_dialog_cancel.setOnClickListener(this);
    }

    /**
     * 点击监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        dismiss();
        switch (id) {
            case R.id.btn_dialog_agree:
                if (onDialogClickListener != null) {
                    onDialogClickListener.agree();
                }
                break;
            case R.id.btn_dialog_cancel:
                if (onDialogClickListener != null) {
                    onDialogClickListener.cancel();
                }
                break;
        }
    }

    public void setCanceledOnTouchOutside(boolean cancel) {
        if (mDialog != null) {
            mDialog.setCanceledOnTouchOutside(cancel);
        }
    }

    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener) {
        this.onDialogClickListener = onDialogClickListener;
    }

    public interface OnDialogClickListener {
        void agree();

        void cancel();
    }
}
